package com.example.usermanagement.service;

import com.example.usermanagement.entity.Book;
import com.example.usermanagement.entity.User;
import com.example.usermanagement.repository.BookRepostitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
@Service
public class BookService {
    private static final Logger logger = Logger.getLogger(BookService.class.getName());//系统自带
    @Autowired
    private BookRepostitory bookRepostitory;
    public Book addBook(Book book) {
        Optional<Book> existingBook = bookRepostitory.findByBookname(book.getBookname());
        if (existingBook.isPresent()) {
            throw new RuntimeException("book " + book.getBookname());
        }

        return  bookRepostitory.save(book);
    }
    public Book updateBook(Book book) {
        return bookRepostitory.save(book);
    }
    public void deleteBook(int id) {
        bookRepostitory.deleteById(id);
    }
    public Optional<Book> getBookById(int id) {
        return bookRepostitory.findById(id);
    }
    public List<Book> getAllBooks() {
        return bookRepostitory.findAll();
    }
}
